#include<fuquOS/gdt.h>

#define GDT_MAX 3

extern void gdt_flush();

struct gdt_entry gdt[GDT_MAX];
struct gdt_ptr gp;

void gdt_set_gate(int num,u32 base,u32 limit,u8 access,u8 granularity){
	gdt[num].limit_low=(limit&0xffff);
	gdt[num].base_low=(base&0xffff);
	gdt[num].base_middle=(base>>16)&0xff;
	gdt[num].access=access;
	gdt[num].granularity=((limit>>16)&0x0f)|((granularity)&0xf0);
	gdt[num].base_high=(base>>24)&0xff;
}

void gdt_init(){
	gp.limit=(GDT_MAX*sizeof(struct gdt_entry))-1;
	gp.base=(u32)&gdt;

	// 设置空的段
	gdt_set_gate(0,0,0,0,0);
	
	// 设置代码段
	gdt_set_gate(1,0,0xFFFFFFFF,0x9a,0xcf);
	// 设置数据段
	gdt_set_gate(2,0,0xffffffff,0x92,0xcf);


	// 加载GDT
	gdt_flush();
}
